import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { LocalStorage } from 'app/ptpmc/localstorage/local.storage';
import { UserService } from 'app/ptpmc/comm/user.service';
import { Router, ActivatedRoute } from '@angular/router';
import { GlobalDataManager } from 'app/ptpmc/comm/global-data-manager';
import { ReportUrls } from 'app/ptpmc/comm/report.urls.conf';
import { CommService } from 'app/ptpmc/comm/comm.service';
import { SubArea } from 'app/ptpmc/model/report/sub-area';
import { UserModel } from '../ptpmc/model/sys/user-model';
import { Notifycation } from '../ptpmc/comm/notifycation';
import { stringify } from 'querystring';

@Component({
    selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

    userId = '';
    password = '';

    enableLoginPage = true;

    constructor(
        private userServices: UserService,
        private ls: LocalStorage,
        private router: Router,
        private http: Http,
        private commService: CommService,
        public route: ActivatedRoute) { }
    ngOnInit() {

                this.enableLoginPage = true;
    }

    accessUser(userModel: any) {
        const token = userModel.token;
        UserModel.roles = userModel.roles;
        UserModel.userId = userModel.userId;
        UserModel.userName = userModel.userName;

        this.ls.set('ptpmc_token', token);
        this.ls.set('ptpmc_userId', userModel.userId);
        this.ls.set('ptpmc_userName', userModel.userName);
        this.ls.set('ptpmc_roles', stringify(userModel.roles));

        /**
         * 改为由rxjs 发送多请求
         */
        this.commService.post(ReportUrls.BaseUrl + ReportUrls.TVResourceUrl, null)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    GlobalDataManager.setTVResources(model.data);
                    this.commService.post(ReportUrls.BaseUrl + ReportUrls.SubAreaUrl, new SubArea())
                        .subscribe(el => {
                            if (el.resultCode === 200) {
                                GlobalDataManager.setSubAreas(el.data);
                                if(userModel.userId=='MC'){
                                    this.router.navigate(['tv/plane']);
                                }else{
                                    this.router.navigate(['tv']);
                                }
                                
                            } else {
                                Notifycation.showNotification('初始化区域失败', 'danger');
                            }
                        });
                } else {
                    Notifycation.showNotification('初始化工位失败', 'danger');
                }
            });
    }

    login() {

        if (this.userId !== '' && this.password !== '') {
            this.userServices.login(this.userId, this.password).subscribe(e => {
                if (e.resultCode === 200) {
                    this.accessUser(e.data);
                } else {
                    alert('登陆失败：' + e.resultMessage);
                }
            });
        } else {
            alert('请输入用户名与密码');
        }

    }
}
